package com.property.cleaning.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.property.cleaning.common.Result;
import com.property.cleaning.entity.Area;
import com.property.cleaning.service.AreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 区域控制器
 * 
 * @author Property Cleaning System
 */
@RestController
@RequestMapping("/api/areas")
@CrossOrigin(origins = "*")
public class AreaController {

    @Autowired
    private AreaService areaService;

    /**
     * 创建区域
     */
    @PostMapping
    public Result<Area> createArea(@RequestBody Area area) {
        return areaService.createArea(area);
    }

    /**
     * 更新区域信息
     */
    @PutMapping("/{areaId}")
    public Result<Area> updateArea(@PathVariable Long areaId, @RequestBody Area area) {
        area.setId(areaId);
        return areaService.updateArea(area);
    }

    /**
     * 删除区域
     */
    @DeleteMapping("/{areaId}")
    public Result<Void> deleteArea(@PathVariable Long areaId) {
        return areaService.deleteArea(areaId);
    }

    /**
     * 根据ID获取区域信息
     */
    @GetMapping("/{areaId}")
    public Result<Area> getAreaById(@PathVariable Long areaId) {
        return areaService.getAreaById(areaId);
    }

    /**
     * 分页查询区域列表
     */
    @GetMapping("/page")
    public Result<IPage<Area>> getAreaPage(@RequestParam(defaultValue = "1") Integer page,
                                           @RequestParam(defaultValue = "10") Integer size,
                                           @RequestParam(required = false) String keyword) {
        Page<Area> pageParam = new Page<>(page, size);
        return areaService.getAreaPage(pageParam, keyword);
    }

    /**
     * 获取区域树形结构
     */
    @GetMapping("/tree")
    public Result<List<Area>> getAreaTree() {
        return areaService.getAreaTree();
    }

    /**
     * 获取子区域列表
     */
    @GetMapping("/{parentId}/children")
    public Result<List<Area>> getChildrenAreas(@PathVariable Long parentId) {
        return areaService.getChildrenAreas(parentId);
    }

    /**
     * 获取所有顶级区域
     */
    @GetMapping("/root")
    public Result<List<Area>> getRootAreas() {
        return areaService.getRootAreas();
    }

    /**
     * 设置区域负责人
     */
    @PostMapping("/{areaId}/manager")
    public Result<Void> setAreaManager(@PathVariable Long areaId,
                                      @RequestParam Long managerId) {
        return areaService.setAreaManager(areaId, managerId);
    }
}